<?php
$dirs = array(
	'app'		=>	'./',
	'model'		=>	'models',
	'web'		=>	'../public',
	'lib'		=>	'../library',
	'conf'		=>	'configs',
	'log'		=>	'../logs',
	'tmp'		=>	'../tmp',
	'view'		=>	'./views'
);


define('APP_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['app'] . DIRECTORY_SEPARATOR);
define('WEB_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['web'] . DIRECTORY_SEPARATOR);
define('MODEL_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['model'] . DIRECTORY_SEPARATOR);
define('LIB_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['lib'] . DIRECTORY_SEPARATOR);
define('LOG_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['log'] . DIRECTORY_SEPARATOR);
define('CONFIG_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['conf'] . DIRECTORY_SEPARATOR);
define('TMP_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['tmp'] . DIRECTORY_SEPARATOR);
define('VIEW_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . $dirs['view'] . DIRECTORY_SEPARATOR);
define('DEBUG',false);

/* Report all errors directly to the screen for simple diagnostics in the dev environment */  
error_reporting(E_ALL | E_STRICT);  
ini_set('display_startup_errors', 1);  
ini_set('display_errors', 1); 
date_default_timezone_set('America/Bogota');
 
set_include_path(LIB_DIR . PATH_SEPARATOR . get_include_path());
set_include_path(MODEL_DIR . PATH_SEPARATOR . get_include_path());
set_include_path(APP_DIR . PATH_SEPARATOR . get_include_path());
set_include_path(TMP_DIR . PATH_SEPARATOR . get_include_path());


//Auto carga de las Clases de Zend
// Cambio de LOADER
require_once (LIB_DIR . 'Zend/Loader/Autoloader.php');
//require_once (LIB_DIR . 'Zend/Loader.php'); 
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->registerNamespace('App_');
//Zend_Loader::registerAutoload();


//Cargar Configuracion
//require_once(APP_DIR . 'config/config.php');
try {
	$config = new Zend_Config_Ini(CONFIG_DIR . 'application.ini', 'default');
} catch (Zend_Exception $e) {
	echo "Error: " . $e->getMessage();
}

Zend_Registry::set("config", $config);

$logfile = $config->general->log->get('path') . $config->general->log->get('filename');
$logDEBUG = $config->general->log->get('path') . "DEBUG" . $config->general->log->get('filename');
require_once 'Zend/Log/Writer/Stream.php';
require_once 'Zend/Log.php';
/** Setup layout */
	require_once 'Zend/Layout.php';


	
	
$logger = new Zend_Log();

/* *** There will be three writers:
 * 		1) The log file with all the messages from WARN up
 * 		2) The DEBUG log file with all the messages
 * 		3) The MAIL destination with all the messages from CRIT up
 */

$writer = new Zend_Log_Writer_Stream($logfile);
$writer->addFilter(Zend_Log::WARN);
$logger->addWriter($writer);
$writer = new Zend_Log_Writer_Stream($logDEBUG);
$logger->addWriter($writer);

/* *** Here is the mail writer *** */
/*
$tr = new Zend_Mail_Transport_Smtp('mail.sigmamovil.com') ;
Zend_Mail::setDefaultTransport($tr);

$mail = new Zend_Mail();
$mail->setFrom('oscar.viana@sigmamovil.com')->addTo('oscar1.viana@gmail.com');

$layout = new Zend_Layout();
$layoutFormatter = new Zend_Log_Formatter_Simple(
    '<li>' . Zend_Log_Formatter_Simple::DEFAULT_FORMAT . '</li>'
);

$writer = new Zend_Log_Writer_Mail($mail, $layout);
$writer->setLayoutFormatter($layoutFormatter);

$writer->setSubjectPrependText('Critical errors on SMS Masivo - ');
$writer->addFilter(Zend_Log::CRIT);

$logger->addWriter($writer);
*/
/* *** Log settings finished *** */

Zend_Registry::set('logger', $logger);

/* Load database */
$options = array(
	Zend_Db::AUTO_QUOTE_IDENTIFIERS => true,
);



/*
$smtpServer	=	$config->general->get('smtpserver');
Zend_Registry::set('smtpServer',$smtpServer);

$urlServer = $config->general->get('urlserver');
Zend_Registry::set('urlServer',$urlServer);
*/
try {
	//conexion BD
	
	$db = Zend_Db::factory($config->database);
	$db->getConnection();
	Zend_Db_Table::setDefaultAdapter($db);
	Zend_Registry::set('database', $db);
	
} 
catch (Zend_Db_Adapter_Exception $e) {
	$logger->log('Database Error: ' . $e->getMessage(), 1);
} 
catch (Zend_Exception $e) {
	$logger->log('Error: ' . $e->getMessage() . $e->getFile() . $e->getTrace() . $e->getCode() . $e->getTraceAsString(), 1);
}

/* Dispatcher */
try {

	
	$frontController = Zend_Controller_Front::getInstance(); 
	$frontController->setControllerDirectory(APP_DIR . "controllers"); 	
	
	$frontController->dispatch();
	
} 
catch (Exception $e) {
	$logger->log($e->getMessage(), 1);
}
